home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / oasis / oasis1-1.lha / oasis-1.1 / load.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  5KB  |  170 lines

  1. /*==========================================================================*
  2.     Oasis Alpha Version 1.1               (C) Copyright 1992 Fah-Chun Cheong
  3.     Revised: 5/1/92 by: fcc@eecs.umich.edu    and The University of Michigan
  4.     ------------------------------------------------------------------------
  5.     Permission to use, copy, modify, distribute, sell and resell Oasis Alpha
  6.     software and its documentation for any purpose and without fee is hereby
  7.     granted, provided that the authorship be appropriately credited and
  8.     acknowledged, and that the above copyright notice appear in all copies
  9.     and both the copyright notice and this permission notice appear in
  10.     supporting documentation. The author makes no representations about the
  11.     suitability of this software for any purpose. It is provided "as is"
  12.     without express or implied warranty. Oasis Alpha is free, caveat emptor!
  13.     ------------------------------------------------------------------------
  14.     To request Oasis Alpha source code:   oasis-alpha-request@eecs.umich.edu
  15.     To enroll in the mailing list:        oasis-alpha-request@eecs.umich.edu
  16.     To send bug reports:                  oasis-alpha-bugs@eecs.umich.edu
  17.     To discuss openly all matters Oasis:  oasis-alpha@eecs.umich.edu
  18.  *==========================================================================*/
  19. #include                <sys/types.h>
  20. #include                <stdio.h>
  21.  
  22. #ifdef  NeXT
  23. #include                <sys/loader.h>
  24. #define FILHDR          struct mach_header
  25. #define FILHSZ          sizeof(FILHDR)
  26. #define SGTHDR          struct segment_command
  27. #define SGTHSZ          sizeof(SGTHDR)
  28. #define SCNHDR          struct section
  29. #define SCNHSZ          sizeof(SCNHDR)
  30. #define TXTHDR          FILHSZ + SGTHSZ
  31. #define DATHDR          FILHSZ + SGTHSZ + SCNHSZ
  32. #endif  NeXT
  33.  
  34. #ifdef  sparc
  35. #include                <a.out.h>
  36. #define FILHDR          struct exec
  37. #define FILHSZ          sizeof(FILHDR)
  38. #endif  sparc
  39.  
  40. #if     defined(apollo) || defined(mips) || defined(_AIX)
  41. #include                <a.out.h>
  42. #ifndef FILHDR
  43. #define FILHDR          struct filhdr
  44. #endif  FILHDR
  45. #ifndef FILHSZ
  46. #define FILHSZ          sizeof(FILHDR)
  47. #endif  FILHSZ
  48. #ifndef SCNHDR
  49. #define SCNHDR          struct scnhdr
  50. #endif  SCNHDR
  51. #ifndef SCNHSZ
  52. #define SCNHSZ          sizeof(SCNHDR)
  53. #endif  SCNHSZ
  54. #define TXTHDR(fh,i)    FILHSZ + fh.f_opthdr + SCNHSZ*i
  55. #define DATHDR(fh,i)    FILHSZ + fh.f_opthdr + SCNHSZ*i
  56.  
  57. #ifdef  apollo
  58. #define TEXT            ".wtext"
  59. #else
  60. #define TEXT            ".text"
  61. #endif  apollo
  62. #define DATA            ".data"
  63. #endif  apollo || mips || _AIX
  64.  
  65. u_long *load_object(name, tseg, dseg)
  66. char   *name;
  67. u_long *tseg;
  68. u_long *dseg;
  69. {
  70.         FILE   *fp;
  71.         int     tsegsz = 0;
  72.         int     dsegsz = 0;
  73.         int     i;
  74.  
  75.         if ((fp = fopen(name, "r")) == NULL) {
  76.             perror("opening object file");
  77.             exit(-1);
  78.         }
  79.  
  80. #if     defined(NeXT)
  81.         {
  82.         SCNHDR  sh;
  83.  
  84.         fseek(fp,   TXTHDR,      0);
  85.         fread(&sh,  SCNHSZ,  1, fp);
  86.         fseek(fp,   sh.offset,   0);
  87.         fread(tseg, sh.size, 1, fp);
  88.         tsegsz    = sh.size;
  89.  
  90.         fseek(fp,   DATHDR,      0);
  91.         fread(&sh,  SCNHSZ,  1, fp);
  92.         fseek(fp,   sh.offset,   0);
  93.         fread(dseg, sh.size, 1, fp);
  94.         dsegsz    = sh.size;
  95.         }
  96. #endif  NeXT
  97.  
  98. #if     defined(sparc)
  99.         {
  100.         FILHDR  fh;
  101.  
  102.         fread(&fh,  FILHSZ,    1, fp);
  103.         fseek(fp,   N_TXTOFF(fh),  0);
  104.         fread(tseg, fh.a_text, 1, fp);
  105.         tsegsz    = fh.a_text;
  106.  
  107.         fseek(fp,   N_DATOFF(fh),  0);
  108.         fread(dseg, fh.a_data, 1, fp);
  109.         dsegsz    = fh.a_data;
  110.         }
  111. #endif  sparc
  112.  
  113. #if     defined(apollo) || defined(mips) || defined(_AIX)
  114.         {
  115.         FILHDR  fh;
  116.         SCNHDR  sh;
  117.  
  118.         fread(&fh, FILHSZ, 1, fp);
  119. #ifdef  LOAD_BUG
  120.         printf("%d sections in COFF file.\n", fh.f_nscns);
  121. #endif  LOAD_BUG
  122.         for (i = 0; i < fh.f_nscns; i++) {
  123.             fseek(fp,  FILHSZ + fh.f_opthdr + SCNHSZ*i, 0);
  124.             fread(&sh, SCNHSZ,  1, fp);
  125.  
  126. #ifdef  LOAD_BUG
  127.             printf("Section #%d: \"%s\"  (%d bytes)\n", i, sh.s_name, sh.s_size);
  128. #endif  LOAD_BUG
  129.             if (strcmp(sh.s_name, TEXT) == 0) {
  130.                 fseek(fp,   TXTHDR(fh, i), 0);
  131.                 fread(&sh,  SCNHSZ,    1, fp);
  132.                 fseek(fp,   sh.s_scnptr,   0);
  133.                 fread(tseg, sh.s_size, 1, fp);
  134.                 tsegsz    = sh.s_size;
  135.             }
  136.             if (strcmp(sh.s_name, DATA) == 0) {
  137.                 fseek(fp,   DATHDR(fh, i), 0);
  138.                 fread(&sh,  SCNHSZ,    1, fp);
  139.                 fseek(fp,   sh.s_scnptr,   0);
  140.                 fread(dseg, sh.s_size, 1, fp);
  141.                 dsegsz    = sh.s_size;
  142.             }
  143.         }
  144.         }
  145. #endif  apollo || mips || _AIX
  146.  
  147.         fclose(fp);
  148. #ifdef  LOAD_BUG
  149.         if (tsegsz > 0) printf("Text Section (%d bytes):", tsegsz);
  150.         for (i = 0; i < tsegsz / 4; i++) {
  151.             if (i % 4 == 0) printf("\n[%08x]  ", tseg + i);
  152.             printf("%08x  ", tseg[i]);
  153.         }
  154.         printf("\n");
  155.         if (dsegsz > 0) printf("Data Section (%d bytes):", dsegsz);
  156.         for (i = 0; i < dsegsz / 4; i++) {
  157.             if (i % 4 == 0) printf("\n[%08x]  ", dseg + i);
  158.             printf("%08x  ", dseg[i]);
  159.         }
  160.         printf("\n");
  161. #endif  LOAD_BUG
  162.  
  163. #ifdef  _AIX
  164.         return dseg;
  165. #else
  166.         return tseg;
  167. #endif  _AIX
  168. }
  169.  
  170.